// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// RESTRICTED USE SOURCE CODE
// http://www.magic-software.com/License/restricted.pdf

#include "MgcDirectionalLight.h"
#include "MgcMatrix3.h"

MgcImplementRTTI(MgcDirectionalLight,MgcLight);
MgcImplementStream(MgcDirectionalLight);

//---------------------------------------------------------------------------
MgcDirectionalLight::MgcDirectionalLight ()
    :
    m_kDirection(MgcVector3::UNIT_Z)
{
}
//---------------------------------------------------------------------------
void MgcDirectionalLight::ComputeDiffuse (const MgcMatrix3& rkWorldRotate,
    const MgcVector3& rkWorldTranslate, MgcReal fWorldScale,
    const MgcVector3* akVertex, const MgcVector3* akNormal,
    unsigned int uiQuantity, const bool* abVisible, MgcColor* akDiffuse)
{
    // transform light direction to model space of old mesh
    MgcVector3 kModelDir = m_kDirection*rkWorldRotate;

    // adjust diffuse color by light intensity
    MgcColor kAdjDiffuse = m_fIntensity*m_kDiffuse;

    for (unsigned int uiV = 0; uiV < uiQuantity; uiV++)
    {
        if ( abVisible[uiV] )
        {
            MgcReal fDot = kModelDir.Dot(akNormal[uiV]);
            if ( fDot < 0.0 )
                akDiffuse[uiV] -= fDot*kAdjDiffuse;
        }
    }
}
//---------------------------------------------------------------------------
void MgcDirectionalLight::ComputeSpecular (const MgcMatrix3& rkWorldRotate,
    const MgcVector3& rkWorldTranslate, MgcReal fWorldScale,
    const MgcVector3* akVertex, const MgcVector3* akNormal,
    unsigned int uiQuantity, const bool* abVisible,
    const MgcVector3& rkCameraModelLocation, MgcColor* akSpecular)
{
    // transform light direction to model space of old mesh
    MgcVector3 kModelDir = m_kDirection*rkWorldRotate;

    // adjust specular color by light intensity
    MgcColor kAdjSpecular = m_fIntensity*m_kSpecular;

    for (unsigned int uiV = 0; uiV < uiQuantity; uiV++)
    {
        if ( abVisible[uiV] )
        {
            MgcReal fDot = kModelDir.Dot(akNormal[uiV]);
            MgcVector3 kReflect = kModelDir - (2.0*fDot)*akNormal[uiV];
            MgcVector3 kViewDir = rkCameraModelLocation - akVertex[uiV];
            fDot = kViewDir.Dot(kReflect);
            if ( fDot > 0.0 )
            {
                MgcReal fCoeff = fDot*fDot/kViewDir.Dot(kViewDir);
                akSpecular[uiV] += fCoeff*kAdjSpecular;
            }
        }
    }
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// streaming
//---------------------------------------------------------------------------
MgcObject* MgcDirectionalLight::Factory (MgcStream& rkStream)
{
    MgcDirectionalLight* pkObject = new MgcDirectionalLight;
    MgcStream::Link* pkLink = new MgcStream::Link(pkObject);
    pkObject->Load(rkStream,pkLink);
    return pkObject;
}
//---------------------------------------------------------------------------
void MgcDirectionalLight::Load (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    MgcLight::Load(rkStream,pkLink);

    // native data
    MgcStreamRead(rkStream,m_kDirection);
}
//---------------------------------------------------------------------------
void MgcDirectionalLight::Link (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    MgcLight::Link(rkStream,pkLink);
}
//---------------------------------------------------------------------------
bool MgcDirectionalLight::Register (MgcStream& rkStream)
{
    return MgcLight::Register(rkStream);
}
//---------------------------------------------------------------------------
void MgcDirectionalLight::Save (MgcStream& rkStream)
{
    MgcLight::Save(rkStream);

    // native data
    MgcStreamWrite(rkStream,m_kDirection);
}
//---------------------------------------------------------------------------
